package com.zscat.mallplus.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zscat.mallplus.active.entity.CmsActive;
import com.zscat.mallplus.active.service.ICmsActiceService;
import com.zscat.mallplus.cms.entity.CmsFavorite;
import com.zscat.mallplus.cms.entity.CmsSubject;
import com.zscat.mallplus.cms.mapper.CmsFavoriteMapper;
import com.zscat.mallplus.cms.service.ICmsSubjectService;
import com.zscat.mallplus.pms.entity.PmsProduct;
import com.zscat.mallplus.pms.service.IPmsProductService;
import com.zscat.mallplus.ums.entity.UmsCollect;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.mapper.UmsCollectMapper;
import com.zscat.mallplus.ums.service.IUmsCollectService;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import com.zscat.mallplus.utils.ValidatorUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zscat
 * @since 2019-04-19
 */
@Service
public class UmsCollectServiceImpl extends ServiceImpl<UmsCollectMapper, UmsCollect> implements IUmsCollectService {

	@Resource
	private UmsCollectMapper collectMapper;

	@Resource
	private IUmsMemberService memberService;

	@Resource
	private IPmsProductService productService;

	@Resource
	private ICmsSubjectService subjectService;

	@Resource
	private ICmsActiceService acticeService;

	@Resource
	private CmsFavoriteMapper favoriteMapper;

	/**
	 * 商品、文章收藏
	 *
	 * @param collect
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object saveCollect(UmsCollect collect) {
		try {
			if (ValidatorUtils.empty(collect.getGoodsId())) {
				return new CommonResult().failed("ID不能为空");
			}
			if (ValidatorUtils.empty(collect.getType())) {
				return new CommonResult().failed("收藏类别不能为空");
			}

			UmsMember currentMember = memberService.getNewCurrentMember();
			if (null != currentMember) {
				collect.setMemberId(currentMember.getId());
			} else {
				return new CommonResult().failed("请先登录");
			}

			UmsCollect findCollect = collectMapper.selectOne(new QueryWrapper<UmsCollect>(collect));
			//collect.getType()   1->商品；2-4>活动商品；5->科普;6->活动；7->研学；8->创课
			if (findCollect == null) {
				if (collect.getType() == 1) {
					// 根据商品ID查询
					PmsProduct product = productService.getById(collect.getGoodsId());
					collect.setGoodsName(product.getName());
					collect.setSubTitle(product.getSubTitle());
					collect.setGoodsPic(product.getPic());
					collect.setGoodsPrice(product.getPrice().toString());
					collect.setCreateTime(new Date());
					collect.setStoreId(product.getStoreId());
					product.setCollectCount(product.getCollectCount() == null ? 1 : product.getCollectCount() + 1);
					productService.update(product, new QueryWrapper<PmsProduct>().eq("id", product.getId()));
				} else if (collect.getType() == 2 || collect.getType() == 3 || collect.getType() == 4) {
					// 根据活动ID查询
					CmsActive active = acticeService.getById(collect.getGoodsId());
					collect.setGoodsName(active.getActiveTitle());
					collect.setGoodsPic(active.getActiveImg());
					collect.setCreateTime(new Date());
					collect.setStoreId(active.getStoreId());
					active.setCollectCount(active.getCollectCount() == null ? 1 : active.getCollectCount() + 1);
					acticeService.update(active, new QueryWrapper<CmsActive>().eq("id", active.getId()));
				} else if (collect.getType() == 5) {
					// 根据文章ID查询
					CmsSubject subject = subjectService.getById(collect.getGoodsId());
					collect.setGoodsName(subject.getTitle());
					collect.setGoodsPic(subject.getPic());
					collect.setCreateTime(new Date());
					collect.setStoreId(subject.getStoreId());
					subject.setCollectCount(subject.getCollectCount() == null ? 1 : subject.getCollectCount() + 1);
					subjectService.update(subject, new QueryWrapper<CmsSubject>().eq("id", subject.getId()));
				}else if(collect.getType() ==6 || collect.getType() ==7 || collect.getType() ==8) {
                	CmsActive active = acticeService.getById(collect.getGoodsId());
                	collect.setGoodsName(active.getActiveTitle());
                	collect.setGoodsPic(active.getActiveImg());
                	active.setCollectCount(active.getCollectCount() == null ? 1 : active.getCollectCount() + 1);
                	acticeService.updateById(active);
                }
				collectMapper.insert(collect);
				return new CommonResult().success("收藏成功");
			} else {
				return new CommonResult().failed("已收藏");
			}
		} catch (Exception e) {
			return new CommonResult().failed(e.getMessage());
		}
	}

	/**
	 * 取消商品、文档收藏
	 *
	 * @param productId
	 * @param type
	 * @return
	 */
	@Transactional(rollbackFor = Exception.class)
	public Object cancelCollect(Long productId, Integer type) {
		try {
			UmsMember currentMember = memberService.getNewCurrentMember();
			collectMapper.delete(
					new QueryWrapper<UmsCollect>().eq("member_id", currentMember.getId()).eq("goods_id", productId).eq("type", type));
			if (type == 1) {
				// 根据商品ID查询
				PmsProduct product = productService.getById(productId);
				product.setCollectCount((product.getCollectCount() == null || product.getCollectCount() == 0) ? 0
						: product.getCollectCount() - 1);
				productService.update(product, new QueryWrapper<PmsProduct>().eq("id", product.getId()));
			} else if (type == 2 || type == 3 || type == 4) {
				// 根据活动ID查询
				CmsActive active = acticeService.getById(productId);
				active.setCollectCount((active.getCollectCount() == null || active.getCollectCount() == 0) ? 0
						: active.getCollectCount() - 1);
				acticeService.update(active, new QueryWrapper<CmsActive>().eq("id", active.getId()));
			} else if (type == 5){
				// 根据文章ID查询
				CmsSubject subject = subjectService.getById(productId);
				subject.setCollectCount((subject.getCollectCount() == null || subject.getCollectCount() == 0) ? 0
						: subject.getCollectCount() - 1);
				subjectService.update(subject, new QueryWrapper<CmsSubject>().eq("id", subject.getId()));
			}else if (type == 6 || type == 7 || type == 8) {
				CmsActive active = acticeService.getById(productId);
				active.setCollectCount((active.getCollectCount() == null || active.getCollectCount() == 0) ? 0
						: active.getCollectCount() - 1);
				acticeService.update(active, new QueryWrapper<CmsActive>().eq("id", active.getId()));
			}
			return new CommonResult().success("取消收藏成功");
		} catch (Exception e) {
			return new CommonResult().failed(e.getMessage());
		}
	}

	@Override
	public Map<String, Object> init(Long goodsId, Integer type, Long memberId) {
		Map<String, Object> map = new HashMap<String, Object>();
		UmsCollect umsCollect = new UmsCollect();
		umsCollect.setGoodsId(goodsId);
		umsCollect.setType(type);
		umsCollect.setMemberId(memberId);
		UmsCollect findUmsCollect = collectMapper.selectOne(new QueryWrapper<UmsCollect>(umsCollect));
		if (type == 1) {
			// 收藏
			if (findUmsCollect == null) {
				map.put("collect", 0);
				map.put("like", 0);
			} else {
				map.put("collect", 1);
				map.put("like", 0);
			}
		} else if (type == 5 || type == 6 || type == 7 || type == 8) {
			// 收藏
			if (findUmsCollect == null) {
				map.put("collect", 0);
			} else {
				map.put("collect", 1);
			}
			// 点赞
			CmsFavorite cmsFavorite = new CmsFavorite();
			cmsFavorite.setObjId(goodsId);
			cmsFavorite.setType(type);
			cmsFavorite.setMemberId(memberId);
			CmsFavorite findCmsFavorite = favoriteMapper.selectOne(new QueryWrapper<CmsFavorite>(cmsFavorite));
			if (findCmsFavorite == null) {
				map.put("like", 0);
			} else {
				map.put("like", 1);
			}
		}else {
			return null;
		}
		return map;
	}

}
